﻿/*
 * d_init_debug(LOGDIR, 0, 0, DEBUG_VERBOSE, DEBUG_VERBOSE, APPNAME);
 * d_destory_debug();
 *
*/

#ifndef _DEBUG_H_
#define _DEBUG_H_    1


#ifndef LOG_TAG
#define LOG_TAG ""
#endif

#ifndef DEBUG_SHOW_DEBUG
#define DEBUG_SHOW_DEBUG    1
#endif

#if defined(__ANDROID_NDK__)

#include <android/log.h>
#if DEBUG_SHOW_DEBUG
#define LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) // 定义LOGD类型
#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGV(...)  __android_log_print(ANDROID_LOG_VERBOSE,LOG_TAG,__VA_ARGS__)
#else //DEBUG_SHOW_DEBUG
#define LOGD(...) do {} while (0)
#define LOGE(...) do {} while (0)
#define LOGI(...) do {} while (0)
#endif //DEBUG_SHOW_DEBUG
#else //__ANDROID_NDK__


#include <stdint.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#if defined(WIN32)
#include <windows.h>
#else
#include <pthread.h>
static unsigned long GetCurrentThreadId()
{
    return pthread_self();
}
#endif



#if defined(__cplusplus)
extern "C" {
#endif


#define DEBUG_NONE          0
#define DEBUG_EMERG         1
#define DEBUG_ALERT         2
#define DEBUG_CRIT          3
#define DEBUG_ERROR         4
#define DEBUG_WARNING       5
#define DEBUG_NOTICE        6
#define DEBUG_INFO          7
#define DEBUG_DEBUG         8
#define DEBUG_VERBOSE       9
#define DEBUG_MIN           DEBUG_NONE
#define DEBUG_MAX           DEBUG_VERBOSE


    /* Define used colors */

    extern const char *g_colortable[];

    enum COLOR{
        NONE,
        KGRN,
        KCYN,
        KRED,
        KYEL,
        KBLU,
        KCYN_L,
        KBRN,
        RESET
    };


#ifdef DEBUG_SHOW_DEBUG
    #define LOGM(format, ...)   __d_debug( DEBUG_NONE,      LOG_TAG, GetCurrentThreadId(), KRED, format "\n", ##__VA_ARGS__ )
    #define LOGF(format, ...)   __d_debug( DEBUG_EMERG,     LOG_TAG, GetCurrentThreadId(), KRED, format "\n", ##__VA_ARGS__ ); assert(0)
    #define LOGC(format, ...)   __d_debug( DEBUG_CRIT,      LOG_TAG, GetCurrentThreadId(), KRED, format "\n", ##__VA_ARGS__ )
    #define LOGE(format, ...)   __d_debug( DEBUG_ERROR,     LOG_TAG, GetCurrentThreadId(), KRED, format "\n", ##__VA_ARGS__ )
    #define LOGW(format, ...)   __d_debug( DEBUG_WARNING,   LOG_TAG, GetCurrentThreadId(), KBRN, format "\n", ##__VA_ARGS__ )
    #define LOGN(format, ...)   __d_debug( DEBUG_NOTICE,    LOG_TAG, GetCurrentThreadId(), KGRN, format "\n", ##__VA_ARGS__ )
    #define LOGI(format, ...)   __d_debug( DEBUG_INFO,      LOG_TAG, GetCurrentThreadId(), KGRN, format "\n", ##__VA_ARGS__ )
    #define LOGD(format, ...)   __d_debug( DEBUG_DEBUG,     LOG_TAG, GetCurrentThreadId(), KBLU, format "\n", ##__VA_ARGS__ )
    #define LOGV(format, ...)   __d_debug( DEBUG_VERBOSE,   LOG_TAG, GetCurrentThreadId(), KCYN, format "\n", ##__VA_ARGS__ )
    #define LOGBIN(data, len)   d_debug_bin_buffer(data, len)
    #define STOPHERE            {LOGD("STOP HERE"); while(true) sleep(1);}
#else
    #define LOGM(format, ...)   do {} while (0)
    #define LOGF(format, ...)   do {} while (0)
    #define LOGE(format, ...)   do {} while (0)
    #define LOGW(format, ...)   do {} while (0)
    #define LOGN(format, ...)   do {} while (0)
    #define LOGI(format, ...)   do {} while (0)
    #define LOGD(format, ...)   do {} while (0)
    #define LOGV(format, ...)   do {} while (0)
    #define LOGBIN(data, len)   do {} while (0)
    #define STOPHERE    
#endif        //#ifdef DEBUG_SHOW_DEBUG


/* debug.c */
extern void d_debug_bin_buffer(const void*data, int len);
/*
logdir: path to logfile dir,must end with /
logfilesize: max Kbyte size of a logfile default not limited
maxlogfilenumber:max number of logfile default 10
level: stdout log level
log:    file log level
binname:file prefix
*/
extern void d_init_debug(const char *logdir, size_t logfilesize, size_t maxlogfilenumber, int level, int log, const char *binname);
extern int  d_set_debug_level(int level);
extern int  d_set_log_level(int level);
extern void __d_debug(int level, const char* tag, const uint64_t pid, const enum COLOR color, const char* format,...);
extern void d_destory_debug();


#if defined(__cplusplus)
}
#endif
#endif //__ANDROID

#endif        // #ifndef _DEBUG_H_
